import Taro from "@tarojs/taro";
import { getBaseUrl, API_CampusRoot, requestSystemUrl } from "./baseUrl";
import interceptors from "./interceptors";
import { getStorage } from '@/pages/components/utils'
import qs from 'qs'
import Utils from '../utils'

const storage = getStorage()

interceptors.forEach((interceptorItem) => Taro.addInterceptor(interceptorItem));


class httpRequest {
    baseOptions(params, method = "GET", ...rest) {
        let { url, data, header, load, cookie,isBackAll } = params;
        const BASE_URL = getBaseUrl();
        let definedHeader = Object.assign({}, {
            "content-type": "application/json;charset=UTF-8",
            "Authorization": (storage.get('token') || Utils.getQueryVariable('lg_tk')),
        }, header);

        let originUrl = url


        if (url.indexOf('http') > -1) {
            url = url
        } else {
            url = BASE_URL + url
        }

        if (method === 'PUT' || method==='DELETE') {
            url += `?${qs.stringify(data)}`
        }

        const option = {
            url: url, //地址
            data: data, //传参
            method: method, //请求方式
            timeout: 50000, // 超时时间
            header: definedHeader,
            loading: load,
            credentials: cookie ? 'include' : 'omit',
            ...rest
        };
        return new Promise((resolve, reject) => {
            Taro.request(option).then((result) => {
                if (result.code == '200' || result.StatusCode == 200 || result.code == 1 || result.error == 0) {
                        if(isBackAll) {
                            resolve(result)
                        }else{
                            resolve(result.data)
                        }
                } else {
                    Taro.showToast({
                        title: result.msg || originUrl + '出错',
                        icon: 'none',
                    })
                }
            }).catch((error) => {
                Taro.showToast({
                    title: error?.desc || '请求出错',
                    icon: 'none',
                })
            });
        });
    }

    get(url, data, header, load, cookie,isBackAll) {
        let option = { url, data, header, load, cookie,isBackAll };
        return this.baseOptions(option);
    }

    post(url, data, header, load) {
        let params = { url, data, header, load };
        return this.baseOptions(params, "POST");
    }

    put(url, data = "", header, load) {
        let option = { url, data, header, load };
        return this.baseOptions(option, "PUT");
    }

    delete(url, data = "", header, load) {
        let option = { url, data, header, load };
        return this.baseOptions(option, "DELETE");
    }

    //校园通代理form表单请求
    transmitFormData(data = {}) {
        const option = {
            url: `${API_CampusRoot}/transmit/doPostForm`, //地址
            data: data, //传参
            method: 'post', //请求方式
            timeout: 50000, // 超时时间
            header: {
                "content-type": "application/json;charset=UTF-8",
            },
            loading: false //可自定义拦截器参数，此loading代表不显示loading框
        };
        return new Promise((resolve, reject) => {
            Taro.request(option).then(resolve).catch(reject);
        })
    }
}

export default new httpRequest();